﻿using HtmlAgilityPack;
using JV.Com.Business;
using JV.Com.Internals;
using JV.Com.Model;
using JV.Com.Tools;
using Microsoft.Practices.Prism.StoreApps.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Documents;

namespace JV.Com.ViewModels
{
    public sealed class PreviewGamePageViewModel : PageBaseViewModel
    {
        private Preview m_preview;
        public PreviewGamePageViewModel(INavigationService navigationService)
            : base(navigationService)
        {
            m_preview = new Preview();
        }

        public event EventHandler PreviewLoadedEvent;
        public async override void OnNavigatedTo(object navigationParameter, Windows.UI.Xaml.Navigation.NavigationMode navigationMode, Dictionary<string, object> viewModelState)
        {
            base.OnNavigatedTo(navigationParameter, navigationMode, viewModelState);

            InLoading = true;
            await LoadPreviewAsync((int)navigationParameter);
            InLoading = false;
        }

        private async Task LoadPreviewAsync(int id)
        {
            m_preview = await Service.GetGamePreviewAsync(id);

            var document = new HtmlDocument();
            document.LoadHtml(m_preview.Content);

            var testContent = document.DocumentNode.ChildNodes.First(c => c.Attributes["id"] != null && c.Attributes["id"].Value == "test_txt");
            var converter = new Tools.HtmlToRichTextConverter(Tools.HtmlToRichTextConverter.ConvertType.Preview);
            var inlinesContent = converter.GetHTMLInlineCollection(testContent.InnerHtml);
            m_contentParagraph = inlinesContent.ToParagraph();

            var recapContent = document.DocumentNode.ChildNodes.FirstOrDefault(c => c.Attributes["id"] != null && c.Attributes["id"].Value == "test_avis");
            if (recapContent != null)
            {
                var allNodes = recapContent.DescendantsAndSelf();
                var preview = allNodes.FirstOrDefault(n => n.Name == "div" && n.Attributes["class"] != null && n.Attributes["class"].Value == "resume");
                if (preview != null)
                {
                    m_preview.GlobalReview = preview.InnerText;
                }
                var note = allNodes.FirstOrDefault(n => n.Name == "img" && n.Attributes["property"] != null && n.Attributes["property"].Value == "v:rating");
                if(note != null)
                {
                    var src = note.Attributes["src"].Value;
                    if(src.EndsWith("tres-bon.png")) m_preview.Rating = PreviewRating.VeryGood;
                    else if (src.EndsWith("bon.png")) m_preview.Rating = PreviewRating.Good;
                    else if (src.EndsWith("moyen.png")) m_preview.Rating = PreviewRating.Medium;
                    else if(src.EndsWith("mauvais.png")) m_preview.Rating = PreviewRating.Bad;
                    else throw new NotSupportedException();
                }
            }

            OnPropertyChanged("GlobalReview");
            OnPropertyChanged("Rating");
            OnPropertyChanged("ImageTopUrl");
            OnPropertyChanged("PageTitle");

            if (PreviewLoadedEvent != null) PreviewLoadedEvent(this, EventArgs.Empty);
        }

        private Paragraph m_contentParagraph;
        public Paragraph ContentParagraph { get { return m_contentParagraph; } }

        public string ImageTopUrl { get { return m_preview.ImageTopUrl; } }
        public PreviewRating Rating { get { return m_preview.Rating; } }
        public string GlobalReview { get { return m_preview.GlobalReview; } }
        public override string PageTitle { get { return m_preview.Title; } }

        #region IFlipViewFullScreenNavigator
        //private IEnumerable<string> m_flipViewImagesUrl;
        //IEnumerable<string> IFlipViewFullScreenNavigator.Images
        //{
        //    get { return m_flipViewImagesUrl; }
        //}

        //void IFlipViewFullScreenNavigator.NavigateToFlipView(IEnumerable<string> urls, int indexToLoad)
        //{
        //    var data = SerializerHelper.Serialize<FlipViewNavigationParameter>(new FlipViewNavigationParameter() { Images = urls, IndexToLoad = indexToLoad });
        //    NavigationService.Navigate("FlipViewFullScreen", data);
        //}

        public void NavigateToFlipView(string imageUrl)
        {
            //var data = SerializerHelper.Serialize<FlipViewNavigationParameter>(new FlipViewNavigationParameter() { Images = urls, IndexToLoad = indexToLoad });
            NavigationService.Navigate("FlipViewFullScreen", imageUrl);
        }
        #endregion
    }
}
